package com.lxmes.sales.service.impl;

import java.util.List;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.sales.domain.AssetClass;
import com.lxmes.sales.mapper.AssetClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.AssetCodeMapper;
import com.lxmes.sales.domain.AssetCode;
import com.lxmes.sales.service.IAssetCodeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产编码Service业务层处理
 *
 * @author lx
 * @date 2024-08-13
 */
@Service
public class AssetCodeServiceImpl implements IAssetCodeService
{
    @Autowired
    private AssetCodeMapper assetCodeMapper;
    @Autowired
    private AssetClassMapper assetClassMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询资产编码
     *
     * @param id 资产编码主键
     * @return 资产编码
     */
    @Override
    public AssetCode selectAssetCodeById(Long id)
    {
        return assetCodeMapper.selectAssetCodeById(id);
    }

    /**
     * 查询资产编码列表
     *
     * @param assetCode 资产编码
     * @return 资产编码
     */
    @Override
    public List<AssetCode> selectAssetCodeList(AssetCode assetCode)
    {
        return assetCodeMapper.selectAssetCodeList(assetCode);
    }

    /**
     * 新增资产编码
     *
     * @param assetCode 资产编码
     * @return 结果
     */
    @Override
    public int insertAssetCode(AssetCode assetCode)
    {
        //分类代码+子类代码，去掉子类代码Z，生成资产代码
        String assetCodeStr = assetCode.getCategoryCode() + assetCode.getSubcategoryCode();
        String newNumber =assetCodeStr+ NewNumber.createNewNumber3(assetCodeMapper.getNewCode(assetCodeStr));
        assetCode.setAssetCode(newNumber);
        assetCode.setAssetNumber(newNumber);
        //编码日期20240813
        assetCode.setCodeDate(DateUtils.dateTime());
        assetCode.setCompanyCode(SecurityUtils.getFactoryCode());
        assetCode.setCompanyName(SecurityUtils.getFactoryName());
        assetCode.setCreateTime(DateUtils.getNowDate());
        assetCode.setCreateBy(SecurityUtils.getUsername());
        return assetCodeMapper.insertAssetCode(assetCode);
    }

    /**
     * 修改资产编码
     *
     * @param assetCode 资产编码
     * @return 结果
     */
    @Override
    public int updateAssetCode(AssetCode assetCode)
    {
        return assetCodeMapper.updateAssetCode(assetCode);
    }

    /**
     * 批量删除资产编码
     *
     * @param ids 需要删除的资产编码主键
     * @return 结果
     */
    @Override
    public int deleteAssetCodeByIds(Long[] ids)
    {
        return assetCodeMapper.deleteAssetCodeByIds(ids);
    }

    /**
     * 删除资产编码信息
     *
     * @param id 资产编码主键
     * @return 结果
     */
    @Override
    public int deleteAssetCodeById(Long id)
    {
        return assetCodeMapper.deleteAssetCodeById(id);
    }
    @Override
    @Transactional
    public String imporIteminfo(List<AssetCode> assetCodeList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(assetCodeList) || assetCodeList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AssetCode assetCode : assetCodeList)
        {
            try
            {
                BeanValidators.validateWithException(validator, assetCode);
                assetCode.setCreateBy(operName);
                assetCode.setUpdateBy(operName);
                AssetClass assetClassQuery=new AssetClass();
                assetClassQuery.setCategoryName(assetCode.getCategoryName());
                assetClassQuery.setSubcategoryName(assetCode.getSubcategoryName());
                AssetClass assetClass = assetClassMapper.selectAssetClassByObj(assetClassQuery);
                if(assetClass==null){
                    throw new ServiceException("资产分类【分类名称:"+assetCode.getCategoryName()+"子类名称:"+assetCode.getSubcategoryName()+"】不存在");
                }
                assetCode.setCategoryCode(assetClass.getCategoryCode());
                assetCode.setSubcategoryCode(assetClass.getSubcategoryCode());
                this.insertAssetCode(assetCode);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
